using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using FetCo.Data;
using ADESSA.Business;
using ADESSA.Entities;
using FetCo.Framework;
using FetCo.Web;

public partial class Admin_AllArticle : System.Web.UI.Page
{
    #region Region
    public int NoCountStart
    {
        get { return Convert.ToInt32(ViewState["NoCountStart"]); }
        set { ViewState["NoCountStart"] = value; }
    }
    protected string hidColSort
    {
        get
        {
            object o = ViewState["hidColSort"];
            if (o == null) return "[" + ADESSA.Entities.Article._Table + "].[" + ArticleColumns.CreatedDate + "]";
            return o.ToString();
        }
        set
        {
            ViewState["hidColSort"] = value;
        }
    }
    public string name
    {
        get
        {
            if (HttpContext.Current.Request.QueryString["name"] == null)
                return "";
            else
                return HttpContext.Current.Request.QueryString["name"];
        }
    }
    public string date
    {
        get
        {
            if (HttpContext.Current.Request.QueryString["date"] == null)
                return "";
            else
                return HttpContext.Current.Request.QueryString["date"];
        }
    }
   
    public long status
    {
        get
        {
            if (HttpContext.Current.Request.QueryString["status"] == null)
                return long.MinValue;
            else
                return long.Parse(HttpContext.Current.Request.QueryString["status"]);
        }
    }
    public long creby
    {
        get
        {
            if (HttpContext.Current.Request.QueryString["creby"] == null)
                return long.MinValue;
            else
                return long.Parse(HttpContext.Current.Request.QueryString["creby"]);
        }
    }
    public long sub
    {
        get
        {
            if (HttpContext.Current.Request.QueryString["sub"] == null)
                return long.MinValue;
            else
                return long.Parse(HttpContext.Current.Request.QueryString["sub"]);
        }
    }

    #endregion
    protected void Page_Load(object sender, EventArgs e)
    {
        if (IsPostBack)
            return;
        LoadCurent();

        LoadCategorySearch();
        LoadddlUserSearch();
        LoadddlStatus(ddlStatusSearch);
        LoadData();
        ddlCategorySearch.SelectedValue = this.sub.ToString();
        ddlStatusSearch.SelectedValue = this.status.ToString();
        ddlUserSearch.SelectedValue = this.creby.ToString();
        txtNameSearch.Text = this.name;
        txtCreateSearch.Text = this.date;
    }
    #region LoadCurent
    private void LoadCurent()
    {
        HtmlGenericControl ulArticle = (HtmlGenericControl)Master.FindControl("ulArticle");
        ulArticle.Attributes.Add("class", "current");
        HtmlGenericControl liSubShowArticle = (HtmlGenericControl)Master.FindControl("liSubShowArticle");
        liSubShowArticle.Attributes.Add("class", "sub_show");
        HtmlControl divSelectArticle = (HtmlControl)Master.FindControl("divSelectArticle");
        divSelectArticle.Attributes.Add("class", "select_sub show");
    }
    #endregion
    #region LoadData
    private string username = string.Empty;
    private void LoadData()
    {
        Criteria cri = new Criteria();
        hyperlinkPager.PageSize = RTISettings.PageSize;
        NoCountStart = (hyperlinkPager.PageSize * (hyperlinkPager.CurrentIndex - 1)) + 1;
        int totalrecord = 0;
        if (!string.IsNullOrEmpty(this.name))
            cri.add("[" + Article._Table + "].[" + ArticleColumns.Title + "]", this.name, Condition.LIKE, Condition.AND);
        if (!string.IsNullOrEmpty(this.date))
            cri.addShortDate("[" + Article._Table + "].[" + ArticleColumns.CreatedDate + "]", Utility.ParseDateTime(this.date), Condition.EQUAL, Condition.AND);
        if (this.sub != long.MinValue && this.sub != 0)
            cri.add("[" + Article._Table + "].[" + ArticleColumns.SubCatId + "]", this.sub, Condition.EQUAL, Condition.AND);
        if (this.creby != long.MinValue && this.creby != 0)
            cri.add("[" + Article._Table + "].[" + ArticleColumns.CreatedBy + "]", this.creby, Condition.EQUAL, Condition.AND);
        if (this.status != long.MinValue && this.status != 0)
            cri.add("[" + Article._Table + "].[" + ArticleColumns.Status + "]", this.status, Condition.EQUAL, Condition.AND);
        ArticleCollection _CompanyCollection = ArticleService.GetArticleListByParam(hidColSort, OrderDirection.ASC, hyperlinkPager.CurrentIndex, hyperlinkPager.PageSize, cri.Criter, out totalrecord);

        rptList.DataSource = _CompanyCollection;
        rptList.DataBind();
        hyperlinkPager.ItemCount = totalrecord;
        if ((float)totalrecord / (float)hyperlinkPager.PageSize <= 1)
            hyperlinkPager.Visible = false;

    }
    protected void rptList_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            Article _Article = (Article)e.Item.DataItem;
            HiddenField hidID = (HiddenField)e.Item.FindControl("hidID");
            HtmlTableRow tr = (HtmlTableRow)e.Item.FindControl("tr");
            Literal ltrTitle = (Literal)e.Item.FindControl("ltrTitle");
            Literal ltrCreateBy = (Literal)e.Item.FindControl("ltrCreateBy");
            Literal ltrCategory = (Literal)e.Item.FindControl("ltrCategory");
            Literal ltrCreateDate = (Literal)e.Item.FindControl("ltrCreateDate");
            HtmlInputText txtOrder = (HtmlInputText)e.Item.FindControl("txtOrder");
            DropDownList ddlStatus = (DropDownList)e.Item.FindControl("ddlStatus");
            ltrTitle.Text = _Article.Title;
            LoadddlStatus(ddlStatus);
            ddlStatus.SelectedValue = _Article.Status.ToString();
            ArticleSubCat _sub = ArticleSubCatService.GetArticleSubCat(_Article.SubCatId);
            if (_sub != null)
            {
                ltrCategory.Text = _sub.Name;
            }
            ADESSA.Entities.User _User = UserService.GetUser(_Article.CreatedBy);
            if (_User != null)
                ltrCreateBy.Text = _User.FullName;
            ltrCreateDate.Text = Utilitys.GetFormattedDate(_Article.CreatedDate);
            txtOrder.Value = _Article.Order.ToString();
            hidID.Value = _Article.ArticleId.ToString();
            int STT = NoCountStart + e.Item.ItemIndex;
            if (STT % 2 != 0)
            {
                tr.Attributes["class"] = "alternate-row";
            }
            else
            {
                tr.Attributes["class"] = "none";
            }
        }

    }
    protected void LoadddlStatus(DropDownList ddlStatus)
    {
        ddlStatus.Items.Clear();
        ddlStatus.Items.Add(new ListItem("---Select---", "0"));
        StatuCollection _StatuCollection = StatuService.GetStatuList(StatuColumns.StatusId, OrderDirection.ASC);
        foreach (Statu _Statu in _StatuCollection)
        {
            ddlStatus.Items.Add(new ListItem(_Statu.StatusName, _Statu.StatusId.ToString()));
        }
    }
    protected void pager_Command(object sender, CommandEventArgs e)
    {
        int currnetPageIndx = Convert.ToInt32(e.CommandArgument);
        hyperlinkPager.CurrentIndex = currnetPageIndx;
        LoadData();
    }
    protected void rptList_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        string commandName = e.CommandName;
        object obj = new object();
        switch (commandName)
        {
            case "sortName":
                obj = "[" + Company._Table + "].[" + CompanyColumns.CompanyName + "]";
                break;
            case "SortOrder":
                obj = "[" + Company._Table + "].[" + CompanyColumns.MemberID + "]";
                break;
            default:
                break;
        }
        if (OrderDirection.ASC == OrderDirection.ASC)
            Session["Order"] = OrderDirection.DESC;
        else
            Session["Order"] = OrderDirection.ASC;
        this.hidColSort = obj.ToString();
        LoadData();
    }
    #endregion
    #region Delete Account
    protected void lndelete_Click(object sender, EventArgs e)
    {
        foreach (RepeaterItem item in this.rptList.Items)
        {
            CheckBox chkDelete = (CheckBox)item.FindControl("chkDelete");
            if (chkDelete.Checked == true)
            {
                HiddenField hidID = (HiddenField)item.FindControl("hidID");
                string strhidID = hidID.Value;
                long ID = long.Parse(strhidID);
                ArticleService.DeleteArticle(ID);
            }
        }
        LoadData();
    }
    #endregion
    #region Update
    protected void linkUpdate_Click(object sender, EventArgs e)
    {
        foreach (RepeaterItem item in this.rptList.Items)
        {
            HiddenField hidID = (HiddenField)item.FindControl("hidID");
            HtmlInputText txtOrder = (HtmlInputText)item.FindControl("txtOrder");
            DropDownList ddlStatus = (DropDownList)item.FindControl("ddlStatus");
            Article _Article = ArticleService.GetArticle(long.Parse(hidID.Value));

            if (_Article != null)
            {
                _Article.Order = int.Parse(txtOrder.Value.Trim());
                _Article.Status = long.Parse(ddlStatus.SelectedValue);

                ArticleService.UpdateArticle(_Article);
            }
        }
        LoadData();
    }
    #endregion

    #region Search
    protected void btnSearch_OnClick(object sender, EventArgs e)
    {
        Response.Redirect(string.Format("AllArticle.aspx?name={0}&date={1}&status={2}&creby={3}&sub={4}", txtNameSearch.Text, txtCreateSearch.Text, ddlStatusSearch.SelectedValue, ddlUserSearch.SelectedValue, ddlCategorySearch.SelectedValue));
    }
    protected void btnSearchAll_OnClick(object sender, EventArgs e)
    {
        Response.Redirect("AllArticle.aspx");
    }
    #endregion




    protected void LoadddlUserSearch()
    {

        ddlUserSearch.Items.Clear();
        ddlUserSearch.Items.Add(new ListItem("---Select---", "0"));
        UserCollection _UserCollection = UserService.GetUserList(UserColumns.UserId, OrderDirection.ASC);
        foreach (User _User in _UserCollection)
        {
            ddlUserSearch.Items.Add(new ListItem(_User.FullName, _User.UserId.ToString()));
        }
    }
    private void LoadCategorySearch()
    {
        ddlCategorySearch.Items.Clear();
        ddlCategorySearch.Items.Add(new ListItem("---Select---", "0"));
        ArticleSubCatCollection _SubCategoryCollection = ArticleSubCatService.GetArticleSubCatList(ArticleSubCatColumns.Code, OrderDirection.ASC);
        foreach (ArticleSubCat _SubCategory in _SubCategoryCollection)
        {
            ddlCategorySearch.Items.Add(new ListItem(_SubCategory.Name, _SubCategory.SubCategoryID.ToString()));
        }
    }
}
